## Chapter 4 (Cross-Party Friendships) 
## Our Common Bonds 
## Winter 2021 - 2022  

## Read in source file 
source("ocb_replication_file.r")

## To get this Pew data, get Waves 15 & 16 of the American Trends Panel: 
## https://www.pewresearch.org/our-methods/u-s-surveys/the-american-trends-panel/ 

pew <- read_sav(file="~/Dropbox/Affective Polarization Book Project/Pew Friendship Data/ATP W16.sav") %>%
  clean_names(.)
pew15 <- read_sav(file="~/Dropbox/Affective Polarization Book Project/Pew Friendship Data/ATP W15.sav") %>%
  clean_names(.)
data_from_wave15 <- pew15 %>% 
  dplyr::select(c(qkey,thermo_a_w15,thermo_b_w15))
## merge with wave 16 
all_data <- left_join(x = pew,
                      y = data_from_wave15,
                      by = "qkey")

## ## Party ID 
all_data <- all_data %>% 
  mutate(pid = ifelse(party_w16 ==2 & partystr_w16 == 1,1,
                      ifelse(party_w16 ==2 & partystr_w16 == 2,2,
                             ifelse(party_w16 >2 & partyln_w16 == 2,3,
                                    ifelse(party_w16 > 2 & partyln_w16 == 3,4,
                                           ifelse(party_w16 > 2 & partyln_w16 == 1,5,
                                                  ifelse(party_w16 == 1 & partystr_w16 == 2,6,
                                                         ifelse(party_w16 == 1 & partystr_w16 == 1,7,NA))))))),
         sp = ifelse(pid==1 | pid==7,1,0),
         dem = ifelse(pid<4,1,0))


## Key outcome/explanatory variables 
all_data <- all_data %>% 
  mutate(harder_friend_other = ifelse(pid < 4,neighbortraita_f1_w16,
                                      ifelse(pid > 4,neighbortraitb_f1_w16,NA)),
         same_therm = ifelse(pid < 4, thermo_b_w15,
                             ifelse(pid > 4,thermo_a_w15,NA)),
         other_therm = ifelse(pid > 4, thermo_b_w15,
                              ifelse(pid < 4,thermo_a_w15,NA)),
         numb_friends_other = ifelse(pid < 4, (-1*friends_idb_w16)+5,
                                     ifelse(pid > 4,(-1*friends_ida_w16)+5,NA)),
         ## remove 5 from original data, is NA
         numb_friends_other = ifelse(numb_friends_other==0,NA,numb_friends_other),
         ## alternative definition excluding leaners 
         alt_friends = ifelse(pid < 3,(-1*friends_idb_w16)+5,
                              ifelse(pid > 5,(-1*friends_ida_w16)+5,NA)),
         alt_friends = ifelse(alt_friends==0,NA,alt_friends),
         party_comp = ifelse(pid < 4,comp_w16,
                             ifelse(pid > 4, (-1*comp_w16)+11,NA)),
         other_good_ideas = ifelse(pid < 4, (-1*ptyidearep_w16)+5,
                                   ifelse(pid > 4, (-1*ptyideadem_w16)+5,NA)),
         ## remove NAs 
         other_good_ideas = ifelse(other_good_ideas==0,NA,other_good_ideas))

## robustness check: w16 candidate therms 
## do averages of Clinton/Sanders, as well as Kasich/Cruz/Trump 
all_data <- all_data %>% 
  rowwise(.) %>% 
  mutate(avg_ft_dem = mean(c(thermo_thermclinton_w16,
                             thermo_thermsander_w16),na.rm=T),
         avg_ft_rep = mean(c(thermo_thermcruz_w16,
                             thermo_thermkasich_w16,
                             thermo_thermtrump_w16), na.rm=T))
all_data <- all_data  %>% 
  mutate(ft_other_cand = ifelse(pid < 4, avg_ft_rep,
                                ifelse(pid > 4, avg_ft_dem,NA)))

## Models  
m0 <- lm(other_therm ~ numb_friends_other, data =all_data)
m1 <- lm(ft_other_cand ~ numb_friends_other, data = all_data)
m2 <- lm(harder_friend_other ~ numb_friends_other, data =all_data)
m3 <- lm(other_good_ideas ~ numb_friends_other, data =all_data)

stargazer(m0,m1,m3,
          column.labels =c("Out-Party FT (w15)",
                           "Out-Candidate FT (w16)",
                           "Other Party Has Good Ideas"),
          covariate.labels = c("Numb. of Cross-Party Friends","Constant"),
          digits = 2, 
          keep.stat = c("n","rsq"),
          type = "html",
          out = "tables/raw output/appendix_table_four_one.html")

## demographics (for prediction of who has x-party friendships)
all_data <- all_data %>% 
  mutate(female = ifelse(f_sex_final ==2,1,0),
         college_grad = ifelse(f_educcat_final < 4,
                               ifelse(f_educcat_final==1,1,0),NA),
         ## race is relative to other (not white/black/Hispanic)
         white = ifelse(f_racethn_recruitment==1,1,0),
         afam = ifelse(f_racethn_recruitment==2,1,0),
         hispanic = ifelse(f_racethn_recruitment==3,1,0),
         married = ifelse(f_marital_final < 3,1,0), 
         ## attends religious services at least 1 or 2 times a month 
         attend_mass = ifelse(f_attend_final < 4,1,0),
         income = ifelse(f_income_final < 10,f_income_final,NA), 
         any_pol_activity = ifelse(civic_eng_actmod_k_w16==1,1,0),
         pol_friends = ifelse(friendinter_w16<3,
                              ifelse(friendinter_w16==1,1,0),NA),
         libcon = ifelse(f_ideo_final < 6,(-1*f_ideo_final)+6,NA), 
         ideologue = abs(libcon-3),
         pid_strength = abs(pid-3),
         sorted = ifelse((pid<4 & libcon <3) | (pid > 4 & libcon > 3),1,0))

m4 <- polr(as.factor(numb_friends_other) ~ dem + pid_strength + 
             ideologue + any_pol_activity, 
           data = all_data)
summary(m4)
m5 <- polr(as.factor(numb_friends_other) ~ dem + pid_strength + 
             ideologue + any_pol_activity + 
             female + college_grad + white + afam + 
             hispanic + income, 
           data = all_data)
summary(m5)
## strong partisans/ideologues are less likely 
## so are Democrats! 
## once you control for those, other measures of engagement don't seem to matter much 
## only demographic effects: 
## AA are less likely, wealthier folks are more likely
## but only race is really a big predictor 

stargazer(m4,m5, 
          column.labels =c("Political Variables Only","Including Demographic Controls"),
          covariate.labels = c("Democrat","Partisan Strength",
                               "Ideological Strength","Politically Participation",
                               "Female","College Graduate",
                               "Caucasian","African-American",
                               "Hispanic","Income"),
          digits = 2, 
          keep.stat = c("n","aic"),
          type = "html",
          out = "tables/raw output/appendix_table_four_two.html")

## Heterogeneous effects by race? 
summary(lm(other_therm ~ numb_friends_other*afam, data =all_data))
summary(lm(ft_other_cand ~ numb_friends_other*afam, data = all_data))
summary(lm(other_good_ideas ~ numb_friends_other*afam, data =all_data))
## no interactive effects 

pdf(file = "figures/chi_levendusky_fig04001.pdf")
ggplot(data = all_data) + 
  geom_bar(aes(x = numb_friends_other, 
               y = 100*..prop..)) + 
  ggtitle("")+
  theme_bw() + 
  ylab("Percentage") + 
  xlab("Number of Cross-Party Friendships") + 
  scale_x_continuous(breaks=c(1:4),
                     labels=c("None","Just a Few",
                              "Some","A Lot")) + 
  theme(plot.title = element_text(hjust=0.5),
        panel.grid = element_blank()) 
dev.off() 

## Graphs to show effects: feeling thermometer ratings 
pdf(file="figures/chi_levendusky_fig04002.pdf")
all_data %>% 
  filter(!is.na(numb_friends_other)) %>% 
  group_by(numb_friends_other) %>% 
  summarise(avg_ft = mean(other_therm, na.rm=T)) %>% 
  ggplot(aes(x=numb_friends_other, y=avg_ft)) + 
  geom_bar(stat="identity") +
  ggtitle("")+
  theme_bw() + 
  ylab("Feeling Thermometer Rating") + 
  xlab("Number of Cross-Party Friendships") + 
  scale_x_continuous(breaks=c(1:4),
                     labels=c("None","Just a Few",
                              "Some","A Lot")) + 
  theme(plot.title = element_text(hjust=0.5),
        panel.grid = element_blank()) 
dev.off()

## Other Party Has Good Ideas 
pdf(file="figures/chi_levendusky_fig04003.pdf")
all_data %>% 
  mutate(agree_good = ifelse(other_good_ideas > 2,1,0)) %>% 
  group_by(numb_friends_other) %>% 
  summarise(pct_agree = mean(agree_good, na.rm=T)) %>% 
  ggplot(aes(x=numb_friends_other, y=100*pct_agree)) + 
  geom_bar(stat="identity") +
  ggtitle("")+
  theme_bw() + 
  ylab("At Least Some Good Ideas (% Agreeing)") + 
  xlab("Number of Cross-Party Friendships") + 
  scale_x_continuous(breaks=c(1:4),
                     labels=c("None","Just a Few",
                              "Some","A Lot")) + 
  theme(plot.title = element_text(hjust=0.5),
        panel.grid = element_blank()) 
dev.off() 